<template>
  <div class="chart"></div>
</template>

<script setup>
import echarts from "@/plugins/echarts";
import { ScatterChart } from "echarts/charts";
import { codeEstimation } from '@/api/dataProcessing'
import { chartData } from './chartData/chartDataD.js'

echarts.use([ScatterChart]);

import { ref, onMounted, computed,defineExpose } from "vue";
const fileName = ref("");
const result = ref("");

const chartYName = ref("齿轮箱振动传感器数据")
const fileDataObj = ref('');
const childStatusObj = ref({
  title: '未清洗',
  status: 500
});

const emit = defineEmits(['childTitleStatusClick']);
emit('childTitleStatusClick', childStatusObj.value)

const props = defineProps({
  fileData: {
    type: String,
    default: ''
  },
  fileName: {
    type: String,
    default: ''
  },
  selectValue: {
    type: Object,
    default () {
      return {}
    }
  }
});

fileName.value = props.fileName;
// base64格式转file对象
const base64ToFile = (base64, fileName) => {
  let arr = base64.split(",");
  // let mime = arr[0].match(/:(.\*?);/)[1];
  let bstr = atob(arr[1]);
  let n = bstr.length;
  let u8arr = new Uint8Array(n);

  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], fileName, { type: '' });
}
fileDataObj.value = base64ToFile(props.fileData, props.fileName)

onMounted(() => {
  getChart();
});

let myChart = ref();
function getChart() {
  var chartDom = document.querySelector(".chart");
  myChart.value = echarts.init(chartDom);
  var option = {
    xAxis: {
      type: "category",
      // data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
    },
    yAxis: {
      name:chartYName.value,
      type: "value",
      nameRotate:0.1,
    },
    legend: {
      right: "15%",
      data: ["齿轮箱振动传感器数据"],
      icon: "roundRect",
      top: "5%",
      textStyle: {
        fontSize: 14
      }
    },
    tooltip: {
      // trigger: 'axis',
      showDelay: 0,
      formatter: function (params) {
        console.log(params)
        if (params.value.length > 1) {
          return (
            params.seriesName +
            " :<br/>" +
            params.value +
            " " +
            params.value +
            " "
          );
        } else {
          return (
            params.seriesName +
            " :<br/>" +
            params.name +
            " : " +
            params.value +
            " "
          );
        }
      },
      axisPointer: {
        show: true,
        type: "cross",
        lineStyle: {
          type: "dashed",
          width: 1
        }
      }
    },
    series: [
      {
        name: '齿轮箱振动传感器数据',
        data: chartData,
        type: "scatter"
      }
    ]
  };

  option && myChart.value.setOption(option);
}

  const childMethod = () => {
    childStatusObj.value.title = "主轴转速异常"
    childStatusObj.value.status = 200;
    emit('childTitleStatusClick', childStatusObj.value)
  }

  // 状态估计
  const childMethod_1 = () => {
    if (fileDataObj.value) {
      childStatusObj.value.title = '估计中....';
      emit('childTitleStatusClick', childStatusObj.value)
      let formData = new FormData()
      formData.append("file", fileDataObj.value)
      codeEstimation(formData).then(res => {
        if (res.status === 200) {
          myChart.value.setOption({
            series: [
              {
                data: res.data.prodata
              }]
          });
          if (res.data.prodata.length > 0) {
            childStatusObj.value.title = "估计完成"
            childStatusObj.value.status = res.status;
            emit('childTitleStatusClick', childStatusObj.value)
            result.value = " 开口机构齿轮出现缺齿故障"
          }
        } else {
          childStatusObj.value.title = "估计失败"
          childStatusObj.value.status = res.status;
          emit('childTitleStatusClick', childStatusObj.value)
          result.value = ""
        }
      }).catch(error => {
        childStatusObj.value.title = '估计失败';
        childStatusObj.value.status = error.status;
        emit('childTitleStatusClick', childStatusObj.value)
        console.log(error)
      });
    }
  }
  defineExpose({
    childMethod
  })

  //计算属性
  // const tagType = computed(() => {
  //   if (b_e_isStop.value ==='未进行状态估计') {
  //     return 'danger'
  //   } else if (b_e_isStop.value ==='状态估计完成') {
  //     return 'success'
  //   }
  // })
</script>
<style lang="scss" scoped>
.dataEstimation {
  .chart {
    width: 100%;
    height: 430px;
  }
}
</style>
